package android.xml;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

public class XmlHandler extends DefaultHandler {

	private List<XmlPojo> list;
	private StringBuilder builder;
	private XmlPojo xmlPojo;
	private Map<String, String> map;

	public List<XmlPojo> getList() {
		return list;
	}

	public XmlHandler() {
		super();
	}

	public XmlHandler(XmlPojo xmlPojo) {
		this.xmlPojo = xmlPojo;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		builder.append(ch, start, length);
		super.characters(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {
		super.endElement(uri, localName, name);
		if (xmlPojo != null) {
			try {
				if (localName.equals("forecast_conditions")) {
					list.add(xmlPojo);
				} else if (localName.equals("condition")) {
					xmlPojo.setCondition(new String(map.get("condition")
							.toString().getBytes("ISO-8859-1"), "GBK"));
					// String datastr = map.get("condition").toString();
					// String temp = new String(datastr.getBytes("ISO-8859-1"),
					// "GBK");
					// try {
					// temp = new String(datastr.getBytes(), "GBK");
					// Log.v("TestCharset","****** getBytes() -> GBK ******\n"+temp);
					// temp = new String(datastr.getBytes("GBK"), "UTF-8");
					// Log.v("TestCharset","****** GBK -> UTF-8 *******\n"+temp);
					// temp = new String(datastr.getBytes("GBK"), "ISO-8859-1");
					// Log.v("TestCharset","****** GBK -> ISO-8859-1 *******\n"+temp);
					// temp = new String(datastr.getBytes("ISO-8859-1"),
					// "UTF-8");
					// Log.v("TestCharset","****** ISO-8859-1 -> UTF-8 *******\n"+temp);
					// temp = new String(datastr.getBytes("ISO-8859-1"), "GBK");
					// Log.v("TestCharset","****** ISO-8859-1 -> GBK *******\n"+temp);
					// temp = new String(datastr.getBytes("UTF-8"), "GBK");
					// Log.v("TestCharset","****** UTF-8 -> GBK *******\n"+temp);
					// temp = new String(datastr.getBytes("UTF-8"),
					// "ISO-8859-1");
					// Log.v("TestCharset","****** UTF-8 -> ISO-8859-1 *******\n"+temp);
					// } catch (UnsupportedEncodingException e) {
					// e.printStackTrace();
					// }
				} else if (localName.equals("low")) {
					xmlPojo.setLow(new String(map.get("low")
							.toString().getBytes("ISO-8859-1"), "GBK"));
				} else if (localName.equals("high")) {
					xmlPojo.setHigh(new String(map.get("high")
							.toString().getBytes("ISO-8859-1"), "GBK"));
				} else if (localName.equals("icon")) {
					xmlPojo.setIcon(new String(map.get("icon")
							.toString().getBytes("ISO-8859-1"), "GBK"));
				} else if (localName.equals("day_of_week")) {
					xmlPojo.setDay(new String(map.get("day_of_week")
							.toString().getBytes("ISO-8859-1"), "GBK"));
				}
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			builder.setLength(0);
		}
	}

	@Override
	public void startDocument() throws SAXException {
		super.startDocument();
		list = new ArrayList<XmlPojo>();
		builder = new StringBuilder();
	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, name, attributes);
		map = new HashMap<String, String>();
		for (int i = 0; i < attributes.getLength(); i++) {
			try {
				Log.d("start", new String(attributes.getValue(i).getBytes("ISO-8859-1"), "GBK"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			map.put(localName, attributes.getValue(i));
		}
		if (localName.equals("forecast_conditions")) {
			xmlPojo = new XmlPojo();
		}
	}
}